﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace Wechat.TenpayV3
{
    /// <summary>
    /// 退款数据
    /// </summary>
    public class RefundData
    {
        readonly JObject jo;
        public RefundData(string json)
        {
            jo = JObject.Parse(json);
        }

        public RefundData(JObject data)
        {
            jo = data;
        }

        /*
         * {
              "refund_id": "50000000382019052709732678859",
              "out_refund_no": "1217752501201407033233368018",
              "transaction_id": "1217752501201407033233368018",
              "out_trade_no": "1217752501201407033233368018",
              "channel": "ORIGINAL",
              "user_received_account": "招商银行信用卡0403",
              "success_time": "2020-12-01T16:18:12+08:00",
              "create_time": "2020-12-01T16:18:12+08:00",
              "status": "SUCCESS",
              "funds_account": "UNSETTLED",
              "amount": {
                "total": 100,
                "refund": 100,
                "from": [...],
                "payer_total": 90,
                "payer_refund": 90,
                "settlement_refund": 100,
                "settlement_total": 100,
                "discount_refund": 10,
                "currency": "CNY"
              },
              "promotion_detail": [...]
            }
         */

        /// <summary>
        /// 退款状态，退款到银行发现用户的卡作废或者冻结了，导致原路退款银行卡失败，可前往服务商平台-交易中心，手动处理此笔退款。
        /// 枚举值（SUCCESS：退款成功，CLOSED：退款关闭，PROCESSING：退款处理中，ABNORMAL：退款异常）
        /// </summary>
        public string Status { get 
            {
                if (jo.ContainsKey("refund_status")) return jo["refund_status"].ToString();
                else if (jo.ContainsKey("status")) return jo["status"].ToString();
                else return string.Empty;
            } }

        /// <summary>
        /// 商户退款单号
        /// </summary>
        public string OutRefundNo { get { return jo["out_refund_no"].ToString(); } }

        /// <summary>
        /// 微信支付退款单号，示例值：50000000382019052709732678859
        /// </summary>
        public string RefundID { get { return jo["refund_id"].ToString(); } }

        /// <summary>
        /// 商户订单号（这里在订单ID前面加了一个前缀字母用于区分交易的商品类型）
        /// </summary>
        public string OutTradeNo { get { return jo["out_trade_no"].ToString(); } }

        /// <summary>
        /// 微信支付系统生成的订单号。 示例值：1217752501201407033233368018
        /// </summary>
        public string TransactionID { get { return jo["transaction_id"].ToString(); } }

        /// <summary>
        /// 退款渠道，枚举值（ORIGINAL：原路退款，BALANCE：退回到余额，OTHER_BALANCE：原账户异常退到其他余额账户，OTHER_BANKCARD：原银行卡异常退到其他银行卡）
        /// </summary>
        public string Channel { get { return jo["channel"].ToString(); } }

        /// <summary>
        /// 退款入账账户，取当前退款单的退款入账方，有以下几种情况：1）退回银行卡：{银行名称}{卡类型}{ 卡尾号}，2）退回支付用户零钱：支付用户零钱，3）退还商户：商户基本账户商户结算银行账户，4）退回支付用户零钱通：支付用户零钱通
        /// </summary>
        public string UserReceivedAccount { get { return jo["user_received_account"].ToString(); } }

        /// <summary>
        /// 退款创建时间
        /// </summary>
        public DateTime CreateTime { get { return DateTime.Parse(jo["create_time"].ToString()); } }

        /// <summary>
        /// 退款成功时间，当退款状态为退款成功时有返回。 示例值：2020-12-01T16:18:12+08:00
        /// </summary>
        public DateTime SuccessTime { get { return DateTime.Parse(jo["success_time"].ToString()); } }

        /// <summary>
        /// 用户支付金额，现金支付金额，单位为分，只能为整数，示例值：90
        /// </summary>
        public int PayerTotal { get { return int.Parse(jo["amount"]["payer_total"].ToString()); } }

        /// <summary>
        /// 订单总金额，单位为分。
        /// </summary>
        public int Total { get { return int.Parse(jo["amount"]["total"].ToString()); } }

        /// <summary>
        /// 用户退款金额，退款给用户的金额，不包含所有优惠券金额，示例值：90
        /// </summary>
        public int PayerRefund { get { return int.Parse(jo["amount"]["payer_refund"].ToString()); } }

        /// <summary>
        /// 退款金额，退款标价金额，单位为分，可以做部分退款
        /// </summary>
        public int Refund { get { return int.Parse(jo["amount"]["refund"].ToString()); } }
    }
}
